<%@include file="/header.jsp"%>
<%@include file="/login/checkLogin.jsp"%>
<%@ page language="java" contentType="text/html" %>
<%@ page import="dataGateway.ItemTable"%>
<%@ page import="domainLogic.Item" %>
<%@ page import="domainLogic.Category" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%
	if (request.getMethod().equals("POST")) {

	    String dir = common.JspUtil.URL_IMAGE;
	    int size = 5 * 1024 * 1024;  //Maximum 5MB
		
	    try{	    
		    MultipartRequest mr = new MultipartRequest(request, 
		    		dir,size,"utf-8",new DefaultFileRenamePolicy());                
	
			String date = mr.getParameter("date");
			String time = mr.getParameter("time");
	
			// Chceck date
			if(date == null || time == null)
			{
				out.println(common.JspUtil.makeErrorMsg("Please input date and time!"));
				return;
			}
			
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy+HH:mm");
			Date inputdate = null;
			try {
				inputdate = formatter.parse(date+"+"+time);
			}
			catch (java.text.ParseException e) {
				out.println(common.JspUtil.makeErrorMsg("Please input date and time correctly!"));
				return;
			}
			
		    //Read created file name
		    String user_id = common.JspUtil.getLoggedInUserId(session);
		    int cat_id = Integer.parseInt(mr.getParameter("catId"));
		    String name = mr.getParameter("name");
		    String description = mr.getParameter("description");	
		    int startingprice = Integer.parseInt(mr.getParameter("startingprice"));	    
		    int reserveprice = Integer.parseInt(mr.getParameter("reserveprice"));	    	    
		    int buyoutprice = Integer.parseInt(mr.getParameter("buyoutprice"));	    
		    Timestamp enddate = new Timestamp(inputdate.getTime());
		    Timestamp realenddate = enddate;
		    String savefilename=mr.getFilesystemName("filename");
 
		    Item newItem = new Item(user_id,cat_id, name, description, startingprice, 
		    		 				reserveprice, buyoutprice, enddate, realenddate, savefilename); 
	 		
		if(newItem.validate(out) == false)
		{
			return;
		}
		
		session.setAttribute("newItem",newItem);
		
	%>
<form action="addItemProcess.jsp" method="POST">
<h3>Item details</h3>
<p>Please check your details!</p>
<table border="2" cellpadding="3" cellspacing="0" style="border-collapse:collapse;" rules="rows" frame="hsides">
	<tr><td colspan="2"></td></tr>
	<tr><td></td>
		<td> <input  type="submit" value="Register"><input  type="button" value="Modify" onClick="history.go(-1)"></td>
	</tr>
	<tr>
		<td>user</td>
		<td><%=newItem.getUserId()%></td>
	</tr>
	<tr>
		<td>category</td>
		<td><%=newItem.getCatId()%></td>
	</tr>
	<tr>
		<td>Item name</td>
		<td><%=newItem.getName()%></td>
	</tr>
	<tr>
		<td>Description</td>
		<td><%=newItem.getDescription()%></td>
	</tr>
	<tr>
		<td>Picture</td>
		<td><%=newItem.getFilename()%></td>
	</tr>	
	<tr>
		<td>Starting Price </td>
		<td><%=newItem.getStartingprice()%></td>
	</tr>
	<tr>
		<td>Reserve Price</td>
		<td><%=newItem.getReserveprice()%></td>
	</tr>
	<tr>
		<td>Buyout Price</td>
		<td><%=newItem.getBuyoutprice()%></td>
	</tr>
	<tr>
		<td>End time</td>
		<td><%=newItem.getEnddate()%></td>
	</tr>
</table>
</form>
<%
	    }catch(Exception e){
	        e.printStackTrace();
	    }
	}
else
{
%>
<html>
<head><title>Create new auction item.</title>
<script type="text/javascript" src="<%=common.JspUtil.URL_CONTENT_PAGE%>/js/itemCheck.js"></script>
</head></head>

<body>
<form name="frm" method="post" enctype="multipart/form-data">
<table bgcolor="#f2dfa9" border="1" align="center">
	<tr>
		<td  colspan="2" align="center">
			<font size="3" color="green"><b>Details</b></font>
		</td>
	</tr>
	<tr>
		<th>user</th>
		<td><%=common.JspUtil.getLoggedInUserName(session)%></td>
	</tr>
	<tr>
		<th>category</th>
		<td>
<select name="catId">
<%
	java.util.List<Category> categories = dataGateway.CategoryTable.getInstance().getCategoriesAll();
for(Category cat : categories)
{
%>
	  <option value="<%=cat.getId() %>"><%=cat.getName()%></option>
<%
}
%>
</select>		
		</td>
	</tr>
	<tr>
		<th> Item name </th>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<th> Description </th>
		<td><textarea name="description" rows="5" cols="40"></textarea></td>
	</tr>
	<tr>
    	<th>File Name </th>
    	<td valign="middle"><input type="file" size= "38" name="filename"></td>
    </tr>
	<tr>
		<th> Starting Price&nbsp;<font size="2" color="Grey">(Optional)</font></th>
		<td><input type="text" name="startingprice"> </td>
	</tr>
	<tr>
		<th> Reserve Price&nbsp;<font size="2" color="Grey">(Optional)</font></th>
		<td><input type="text" name="reserveprice"></td>
	</tr>
	<tr>
		<th> Buyout Price&nbsp;<font size="2" color="Grey">(Optional)</font></th>
		<td><input type="text" name="buyoutprice"></td>
	</tr>
	<tr>
		<th> End date </th>
		<td>DD/MM/YYYY : <input type="text" name="date"></td>
	</tr>
	<tr>
		<th> End time </th>
		<td>HH:MM<input type="text" name="time"></td>
	</tr>
	<tr>
		<td colspan="2"><center>
			<input type="button" value="Register" onClick="checkForm()">&nbsp;&nbsp;
			<input type="reset" value="Cancel"></center></td>
	</tr>
</table>
</form>
</body>
</html>
<% 
} %>
<%@include file="/footer.jsp"%>